<template>
  <el-dialog
    v-el-drag-dialog
    title="编辑机构监管"
    :visible.sync="visible"
    width="500px"
    @closed="reset"
  >
    <el-form ref="dataForm" class="form" :model="temp" label-position="right" label-width="140px">
      <el-form-item label="现金留存预警:" prop="cashTopLimit">
        <el-input-number v-model.trim="temp.cashTopLimit" :controls="false" :min="0" :max="999999999999999" :precision="0" clearable style="width: 100%;" />
      </el-form-item>
      <el-form-item label="月结减免差额上限:" prop="monthlyTopLimit">
        <el-input-number v-model.trim="temp.monthlyTopLimit" :controls="false" :min="0" :max="999999999999999" :precision="0" clearable style="width: 100%;" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.submit" type="primary" @click="success">
        保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { updateOrgSuperviseSetting } from '@/api'

  export default {
    name: 'update',
    props: {
      treeData: {
        type: Array,
        required: true
      },
      orgList: {
        type: Array,
        required: true
      }
    },
    data() {
      return {
        visible: false,
        temp: {
          cashTopLimit: undefined,
          monthlyTopLimit: undefined
        }
      }
    },
    methods: {
      open(temp) {
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.temp = Object.assign(this.temp, temp)
          }
        })
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          const params = {
            cashTopLimit: this.temp.cashTopLimit,
            monthlyTopLimit: this.temp.monthlyTopLimit,
            orgId: this.temp.orgId
          }
          this.submit(params)
        })
      },
      async submit(params) {
        await updateOrgSuperviseSetting(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '更新成功',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.temp = {
          cashTopLimit: undefined,
          monthlyTopLimit: undefined
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.form {
  display: flex;
  flex-wrap: wrap;
  & /deep/ .el-form-item {
    width: 100%;
  }
}
</style>
